#include <ez8.h>
#include "spi.h"

void TransmitSPI(void)
//waits for transmission to end
{
	while((SPISTAT & TXST) == 0x00);		//Wait until transmission	
	while((SPISTAT & TXST) == TXST); 		//Wait until end of transmission
}

unsigned char TransferSPI(unsigned char data)
//simultaneously transfers data from master to slave and from slave to master (from host to card and from card to host)
{
	SPIDATA = data;
	TransmitSPI();
	data = SPIDATA;
	return data;
}

void InitSPI(void) 
//initializes the SPI
{
	//SPI pins alternate function
	PCADDR = ALT_FUN;
	PCCTL |= PIN_ALT;
	PCADDR = 0x00;

	//SPI settings
	SPICTL = (PHASE|CLKPOL|MASTER|SPI_ENABLE);
	SPIMODE = (NUMBIT8|SSIO|SS_HIGH);
	
	//Baud Rate Generator
	SPIBRH = BAUD_H;
	SPIBRL = BAUD_L;

}

void AssertSS()
//selects the MMC (slave)
//used at the start of an SPI transaction
{
	SPIMODE &= ~SS_HIGH; 			// SS pin low
}


void DeassertSS()
//deselects the MMC(slave)
//used at the end of an SPI transaction
{
	SPIMODE |= SS_HIGH; 			// SS pin high
}
